package com.situ.ssh.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.ssh.common.ServerResponse;
import com.situ.ssh.entity.Student;
import com.situ.ssh.service.IStudentService;
import com.situ.ssh.util.JSONUtil;
import com.situ.ssh.util.SearchCondition;

import net.sf.json.util.JSONUtils;

@Controller
@Scope("prototype")
public class StudentAction extends BaseAction<Student>  {
	@Autowired
	private IStudentService studentService;
	
	
	public String list() {
		List<Student> list =studentService.findAll();
		//放到域对象
		requestMap.put("list", list);
		return "list";
	}
	
	public String pageList() {
		//离线DetachedCriteria中封装查询条件
		String name = model.getName();
		if (StringUtils.isNotEmpty(name)) {
			// 添加过滤条件，根据名字关键字来模糊查找
			detachedCriteria.add(Restrictions.like("name", "%" + name + "%"));
		}
		searchCondition.setDetachedCriteria(detachedCriteria);
		ServerResponse serverResponse = studentService.pageList(searchCondition);
		
		JSONUtil.obj2Json(serverResponse);
		/**
	     * The action execution was successful but do not
	     * show a view. This is useful for actions that are
	     * handling the view in another fashion like redirect.
	     */
		return NONE; // none
	}
	
	public String delete() {
		ServerResponse serverResponse = studentService.delete(model);
		JSONUtil.obj2Json(serverResponse);
		/**
	     * The action execution was successful but do not
	     * show a view. This is useful for actions that are
	     * handling the view in another fashion like redirect.
	     */
		return NONE; // none
	}
	
	public String deleteAll() {
		System.out.println(ids);
		ServerResponse serverResponse = studentService.deleteAll(ids);
		JSONUtil.obj2Json(serverResponse);
		/**
	     * The action execution was successful but do not
	     * show a view. This is useful for actions that are
	     * handling the view in another fashion like redirect.
	     */
		return NONE; // none
	}

}
